import pandas as pd

from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_absolute_error

# 读取数据
data = pd.read_csv('data/data3.csv')

# 查看数据
print(data.head())

# 数据清洗，填充缺失值
data.ffill()

data['ds'] = pd.to_datetime(data['ds'])
data['year'] = data['ds'].dt.year
data['week'] = data['ds'].dt.strftime("%W")

# 定义特征和目标
features = data[['year', 'week']]
target = data['y']

# 划分数据集
X_train, X_test, y_train, y_test = train_test_split(features, target, test_size=0.2, random_state=42)

# 训练随机森林模型
model = RandomForestRegressor(n_estimators=100)
model.fit(X_train, y_train)

# 评估模型
predictions = model.predict(X_test)
mae = mean_absolute_error(y_test, predictions)
print(f'Mean Absolute Error: {mae}')

# 准备预测数据，这里假设我们预测未来7天的销量
future_dates = pd.date_range(start='2025-01-01', periods=52, freq='7D')
future_data = pd.DataFrame({'year': future_dates.year, 'week': future_dates.strftime("%W")})

# 进行预测
future_predictions = model.predict(future_data)
print('未来7天的销量预测:', future_predictions)
